﻿namespace Oddo.Concept.Crm.Client.Repository.ViewModels
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using Microsoft.Practices.Prism.UnityExtensions;
    using Oddo.Concept.Crm.Common.Presentation.Interfaces;
    using Oddo.Concept.Crm.Common.ViewModels;
    using Oddo.Concept.Crm.Models;
    using Oddo.Concept.Crm.Repository.Common.Contracts;
    using Microsoft.Practices.Unity;

    public interface ICountrySearchView : IBaseView
    {
    }

    public class CountryViewModel : ViewModelBase<Country>
    {
        public CountryViewModel(){}

        [Dependency]
        public IRepositoryServices RepositoryServicesClient { get; set; }

        protected override void SearchCommandHandler()
        {
            IsBusy = true;

            InitializeCancellationTokenSource();

            RepositoryServicesClient.FindCountriesAsync((CountryFindRequest) FindRequest,base.CancellationTokenSource.Token)
                                    .ContinueWith(r =>
                                        {
                                            if (!r.IsFaulted && r.Result.Exception == null)
                                                this.Values = new ObservableCollection<Country>(r.Result.Values ?? new List<Country>());
                                            IsBusy = false;

                                        }, base.CancellationTokenSource.Token);
        }

        protected override void ResetCriteriasCommandHandler()
        {
            ((CountryFindRequest)FindRequest).Code = null;
            ((CountryFindRequest)FindRequest).Name = null;
        }

        protected override void AddCommandHandler()
        {
        }

        protected override void EditCommandHandler()
        {
        }

        protected override void DeleteCommandHandler()
        {
        }

        protected override void CancelCommandHandler()
        {
            base.CancelCommandHandler();
        }

        public override void InitializeCancellationTokenSource()
        {
            base.InitializeCancellationTokenSource();
        }
    }
}